# -*- coding:utf-8 -*-

# /* 拼多多店铺评分 */



from common.config import *
import requests,json
from common.Helper import Helper

class PddScore( object ):

    def __init__(self):

        pass

    # 页面请求
    def requestIndex(self,task):

        resp = {'type':106,'msg':'店铺评分','content':{}}

        # 设置cookie
        header['cookie'] = task['cookie']

        data = {}

        r = requests.post( url=domain_url['score_url'],data=json.dumps( data ),headers=header)

        # 整体判断错误信息
        if r.status_code == 200 and r.json()['success'] == False:

            warn = dict( error_msg= r.json()['error_msg'],error_code=r.json()['error_code'])

            resp['content']['content'] = task
            resp['content']['errcode'] = warn['error_code']
            resp['content']['errmsg'] = warn['error_msg']


            content = Helper.changeBase64(json.dumps(resp['content']))

            resp['content'] = content.decode('ascii')

            return resp


        elif r.status_code == 200 and r.json()['result']:

            items = r.json()['result']

            data = self.parseIndex(items)

            resp['content']['content'] = task
            resp['content']['data'] = (data)
            resp['content']['errcode'] = 0
            resp['content']['errmsg'] = '任务完成'

            print( resp )

            content = Helper.changeBase64(json.dumps(resp['content']))

            resp['content'] = content.decode('ascii')

            return resp


            # return data
        else:

            # 记录错误日志
            pass

            return None

    # 解析信息
    def parseIndex(self,items):

        data = {

            "descOver50RevScr3m":round(items['descScore'],2),
            "descOver50LgstRevScr3m":round(items["logisticsScore"],2),
            "descOver50ServRevScr3m":round(items["serviceScore"],2),
            "avgDescRevScrRcatePct3m":round(items["avgDescRevScrRcatePct3m"],2),
            "avgLgstRevScrRcatePct3m":round(items["avgLgstRevScrRcatePct3m"],2),
            "avgServRevScrRcatePct3m":round(items["avgServRevScrRcatePct3m"],2)
        }

        return data

'''
descOver50RevScr3m
descOver50LgstRevScr3m
descOver50RevScr3m
'''

if __name__ == '__main__':

    task = {"task_id":1,"owner_id":186716817,"state":0,"data_date":"2019-09-21","add_time":"2019-09-22 16:50:02","cookie":"api_uid=rBQRGV0ufMgJpwYt0e1qAg==; _nano_fp=XpdjXqXxnqEbnpXyXT_g30sJEPJz34pSjjxCPZK3; PASS_ID=1-5+psRVgH2Ux2NsbXWlW6Qwn37KPyb5nAQvKS6reoE0bz\/L\/nH7npxNpBbepWlFPKNjpTCUw1VEeocNLnwAwZ5Q_186716817_29159208; JSESSIONID=79864EB26BE7638672AC9673B2F83640"}

    s = PddScore()

    s.requestIndex( task )












